import { getCityCode, getWeatherData, getHmWeatherData } from '@/api/weather.js'

export default {
	namespaced: true,
	state() {
		return {
			// 天气数据
			weather: ''
		}
	},
	mutations: {
		// 赋值weather
		getWeather(state, res) {
			console.log('结果：', res)
			state.weather = res.data.data
		}
	},
	getters: {},
	actions: {
		// 异步获取高德api数据
		async getWeatherActions(context, city) {
			const { data: res } = await getWeatherData(city)

			if (res.infocode !== '10000') return uni.$showMsg('请求天气预报数据错误！')
			context.commit('getWeather', res)
		},

		// 黑马获取城市id
		async getCityCodeActions(context) {
			// const res = await getCityCode()
			console.log(res)
			wx.request({
				url: 'http://hmajax.itheima.net/api/weather/city?city=深圳',
				method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				success: function (res) {
					// success
				}
			})
		},
		// hm 天气数据
		async getHmWeatherActions(context) {
			// const res = await getHmWeatherData()
			// const result = res
			wx.request({
				url: 'https://hmajax.itheima.net/api/weather?city=440300',
				method: 'GET',
				success: res => {
					context.commit('getWeather', res)
				}
			})
			// console.log(result);
			// context.commit('getWeather', result)
		}
	}
}
